<template>
  <div>
    <div class="row">
      <label class="col-sm-2 control-label">时间</label>
      <div class="col-sm-4">
        <MyDatePicker :editable='true' v-bind:date='time' ref="belongTime" @timeChanged="(time) => this.time = time"></MyDatePicker>
      </div>
      <label class="col-sm-2 control-label">完成率</label>
      <div class="col-sm-4 control-label">
        <p>{{ statRate}}</p>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-6 table-responsive">
        <div class='box box-success'>
          <div class='ibox-title'>
            <h5>
              <i class='fa fa-pie-chart'></i>&nbsp;&nbsp;{{ statPieTitle }}</h5>
          </div>
          <div class='ibox-content'>
            <canvas id='profChart'></canvas>
          </div>
        </div>
      </div>
      <div class="col-sm-6 table-responsive">
        <el-tabs v-model="activeName" type="border-card">
          <el-tab-pane label="未填报单位" name="unstats">
            <section class="content" id="unstats">
              <div class="row">
                <el-table stripe :highlight-current-row='true' :data="showUnStat" border style="width: 100%">
                  <el-table-column :prop='name' width="400" :label='unprop'></el-table-column>
                </el-table>
              </div>
              <el-pagination @current-change="handleUnstatPageChange" :current-page.sync="unstatPage" :page-size="10" layout="total, prev, pager, next" :total="unTotal">
              </el-pagination>
            </section>
          </el-tab-pane>
          <el-tab-pane label="已填报单位" name="stats">
            <section class="content" id="stats">
              <div class="row">
                <el-table stripe :highlight-current-row='true' :data="showStats" border style="width: 100%">
                  <el-table-column :prop='name' width="400" :label='statProp'></el-table-column>
                </el-table>
              </div>
              <el-pagination @current-change="handleStatPageChange" :current-page.sync="statPage" :page-size="10" layout="total, prev, pager, next" :total="statTotal">
              </el-pagination>
            </section>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>

<script>
  import monthApi from "../../../common/monthapi";
  import MyDatePicker from "../../mycomponents/common/MyDatePicker";
  import Utils from "../../../common/utils";
  import Chart from 'chart.js'
  let api = new monthApi();
  export default {
    name: "stat",
    components: {
      MyDatePicker
    },
    created: function() {
      this.time = api.getThisMonth();
      // this.thismonth.belongTime = mydate;
      this.loadStat(this.time);
    },
    data: function() {
      return {
        statPieTitle: '上报统计图',
        activeName: 'unstats',
        time: "",
        rate: 0.0,
        statRate: [],
        unStat: [],
        stat: [],
        showStats: [],
        showUnStat: [],
        statProp: '已填报单位',
        unprop: '未填报单位',
        unTotal: 0,
        name: 'name',
        unstatPage: 1,
        statTotal: 0,
        statPage: 1,
        profChart: null,
        // pieDataLabels: ['已填报', 　'未填报'],
        pieData: [],
        config: {
          type: 'pie',
          data: {
            labels: ['已填报', 　'未填报'],
            datasets: [{
              label: '填报状态统计',
              data: this.pieData,
              backgroundColor: ['#00a65a', '#dd4b39', '#f39c12', '#00c0ef'],
              hoverBackgroundColor: ['#00a65a', '#dd4b39', '#f39c12', '#00c0ef']
            }]
          },
          options: {
            responsive: true,
            maintainAspectRatio: true,
            legend: {
              position: 'bottom',
              display: true
            }
          }
        }
      }
    },
    watch: {
      unTotal: {
        handler(curval, oldval) {
          this.updatePie()
          console.log("unTotal changed:" + curval);
        }
      },
      time: {
        handler(curval, oldval) {
          this.loadStat(curval);
          console.log("stat time changed:" + curval);
        },
        deep: true
      }
    },
    mounted() {
      this.showPie()
    },
    methods: {
      showPie() {
        var profChart = document.getElementById('profChart').getContext('2d')
        this.profChart = new Chart(profChart, this.config)
      },
      handleUnstatPageChange() {
        if (this.unstatPage > 1) {
          this.showUnStat = this.unStat.slice((this.unstatPage - 1) * 10, this.unstatPage * 10)
        } else {
          this.showUnStat = this.unStat.slice(0, 10)
        }
      },
      handleStatPageChange() {
        if (this.statPage > 1) {
          this.showStats = this.stat.slice((this.statPage - 1) * 10, this.statPage * 10)
        } else {
          this.showStats = this.stat.slice(0, 10)
        }
      },
      loadStat(time) {
        api.getStat(time, resp => {
          this.statRate = resp.body.body.statRate
          this.stat = this.parse(resp.body.body.stat)
          this.showStats = this.stat.slice(0, 10)
          this.statTotal = this.stat.length
          this.unStat = this.parse(resp.body.body.unStat)
          this.showUnStat = this.unStat.slice(0, 10)
          this.unTotal = this.unStat.length
          this.pieData[0] = this.statTotal
          this.pieData[1] = this.unTotal
          this.config.data.datasets[0].data = this.pieData
        })
      },
      updatePie() {
        this.profChart.update(this.config)
      },
      parse(array) {
        var container = []
        for (var index = 0; index < array.length; index++) {
          container.push({
            name: array[index]
          })
        }
        return container
      }
    }
  };
</script>

<style>
  .table>thead:first-child>tr:first-child>th {
    border-top: 1px solid #352f2f;
  }
  .table-bordered>tbody>tr>td,
  .table-bordered>tfoot>tr>td {
    border: 1px solid #352f2f;
  }
  .table-bordered>thead>tr>th,
  .table-bordered>tbody>tr>th,
  .table-bordered>tfoot>tr>th,
  .table-bordered>thead>tr>td,
  .table-bordered>tbody>tr>td,
  .table-bordered>tfoot>tr>td {
    border: 1px solid #352f2f;
  }
  .row {
    padding: 10px 10px;
  }
  .control-label {
    padding-top: 10px;
  }
  .red {
    border-color: "red";
    color: "red";
  }
</style>
